package com.dongdongshop.controller;

import cn.hutool.core.lang.Validator;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.model.TbUser;
import com.dongdongshop.domain.vo.TbUserVO;
import com.dongdongshop.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@CrossOrigin  //跨域
@Api("卖家管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //根据username查询数据
    @PostMapping("userByName")
    public Result userByName(@RequestParam String username) {
        TbUserVO userVO = userService.userByName(username);
        return Result.OK().setData(userVO);
    }

    //检查用户名是否重复
    @PostMapping("selectUserName")
    public Result selectUserName(@RequestParam String username) {
        try {
            userService.selectUserName(username);
            return Result.OK();
        } catch (Exception e) {
            return Result.FALL().setData("用户名重复, 请重新输入");
        }
    }


    //注册
    @PostMapping("addUser")
    public Result addUser(@RequestBody TbUserVO userVO) {
        return userService.addUser(userVO);
    }

    //mq生产者
    @PostMapping("sendPhoneMessage")
    public Result sendPhoneMessage(String phoneNum) {
        boolean mobile = Validator.isMobile(phoneNum);
        if(!mobile){
            return Result.FALL().setData("手机号码错误");
        }
        rocketMQTemplate.convertAndSend("dk2208-topic",phoneNum);
        return Result.OK();
    }



}
